﻿/*  
    file: GenerateRequestUrl.js
    require: Utilities.js
*/
!function (ns) {
    var facebookAuth = {
        url: "http://www.facebook.com/dialog/oauth",
        display: "popup",
        client_id: "114688978554228",
        scope: "email,read_stream,offline_access",
        redirect_uri: "{0}"
    };
    var port = location.port;
    port = port ? ":" + port : "";
    var redirectUrlMap = {
        "114688978554228": "\/\/local.englishtown.com",
        "112952288740188": "\/\/qa.englishtown.com",
        "118306954855808": "\/\/staging.englishtown.com",
        "116849458338418": "\/\/www.englishtown.com"
    };
    var facebookApiParams = ["client_id",
                             "redirect_uri",
                             "scope",
                             "state",
                             "response_type",
                             "display"];

    function generateUrl(urlObj, paramsArray) {
        var url = urlObj.url;
        var params = [];
        var n = 0, len;
        if (etui.n.type(paramsArray) === "Array") {
            for (n = 0, len = paramsArray.length; n < len; n++) {
                if (urlObj[paramsArray[n]]) {
                    params.push(paramsArray[n] + "=" + urlObj[paramsArray[n]]);
                }
            }
        }
        else {
            for (n in urlObj) {
                if (urlObj.hasOwnProperty(n) && n !== "url") {
                    params.push(n + "=" + urlObj[n]);
                }
            }
        }
        url += url.indexOf("?") > -1 ? "&" : "?";
        url += params.join("&");
        return url;
    }

    function GenerateRequestUrl(options) {
        this.facebookAuthUrl = generateUrl(facebookAuth, facebookApiParams);
        this.facebookAuthInit = {};
        var len, name, value;
        for (len = facebookApiParams.length; len--; ) {
            name = facebookApiParams[len];
            value = options[name] || facebookAuth[name];
            if (value) {
                this.facebookAuthInit[name] = value;
            }
        }
    }
    //
    // 3 parameters
    //
    GenerateRequestUrl.prototype.getUrl = function (withFacebookAuth, actionObj /*, [additionalData] */) {
        var additionalData;
        var name;
        var redirectUrl = window.location.protocol + redirectUrlMap[this.facebookAuthInit.client_id] + "\/facebook\/fbreceiver.aspx";
        var param = {
            url: redirectUrl,
            np: actionObj.senderPage,
            rn: actionObj.receiverName,
            ac: actionObj.actionName
        };
        if (arguments.length === 3) {
            additionalData = arguments[2];
            for (name in additionalData) {
                if (additionalData.hasOwnProperty(name)) {
                    param[name] = additionalData[name];
                }
            }
        }
        var redirectUrl = generateUrl(param);
        if (withFacebookAuth) {
            redirectUrl = etui.n(this.facebookAuthUrl).stringFormat(encodeURIComponent(redirectUrl));
        }
        return redirectUrl;
    };
    ns.GenerateRequestUrl = GenerateRequestUrl;
} (etui.ui);